package com.hisan.freeride.home.activity;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.DividerItemDecoration;


import com.hisan.freeride.R;
import com.hisan.freeride.common.base.AppConfig;
import com.hisan.freeride.common.base.BaseActivity;
import com.hisan.freeride.common.callback.DialogCallback;
import com.hisan.freeride.common.utils.CollectionUtils;
import com.hisan.freeride.common.view.MyLinearLayoutManager;
import com.hisan.freeride.databinding.LoveDetailsBinding;
import com.hisan.freeride.home.adapter.PriceAdapter;
import com.hisan.freeride.home.model.Loveprice;
import com.lzy.okgo.OkGo;


import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 创建时间 : 2017/6/6
 * 创建人：yangyingqi
 * 公司：嘉善和盛网络有限公司
 * 备注：爱心币明细页面
 */
public class LovePriceActivity extends BaseActivity<LoveDetailsBinding> {
    private int pageNumber=1;
    private PriceAdapter priceAdapter;
    private Intent intent;
    @Override
    protected int getLayoutId() {
        return R.layout.love_details;
    }


    @Override
    protected void initView() {
        mBinding.setStateModel(mStateModel);
        priceAdapter=new PriceAdapter(new ArrayList<>(0));
        mBinding.rv.setLayoutManager(new MyLinearLayoutManager(this));
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.shape_drive_line));
        mBinding.rv.addItemDecoration(dividerItemDecoration);
        mBinding.newMs.setOnClickListener(v -> RotationActivity.skip(LovePriceActivity.this, AppConfig.exprule));
        mBinding.rv.setAdapter(priceAdapter);
        mBinding.triporder.setOnClickListener(v -> {
            if (CollectionUtils.isNullOrEmpty(intent))intent=new Intent();
            setResult(98,intent);
            finishActivity(true);
        });
        mBinding.exchange.setOnClickListener(v -> startActivityForResult(ExchangeActivity.class,0,null,true));

    }

    @Override
    protected void initEvent() {
        mBinding.topBack.setOnClickListener(v -> finishActivity(true));
    }

    @Override
    protected void loadData(boolean isRefresh) {
        OkGo.<Loveprice>get(AppConfig.Mycoin)
                .tag(this)
                .params("pageNumber",pageNumber)
                .params("pageSize",10)
                .execute(new DialogCallback<Loveprice>(this) {
                    @Override
                    public void onSuccess(com.lzy.okgo.model.Response<Loveprice> response) {
                        super.onSuccess(response);
                        if (!CollectionUtils.isNullOrEmpty(response.body())){
                            mBinding.price.setText("爱心值  "+response.body().getCoin());
                         if (!CollectionUtils.isNullOrEmpty(response.body().getList())){
                             showData();
                             if (!CollectionUtils.isNullOrEmpty(response.body().getList()))
                                 priceAdapter.setNewData(response.body().getList());
                         }else{
                             noData();
                         }
                        }
                    }
                    @Override
                    public void onError(com.lzy.okgo.model.Response<Loveprice> response) {
                        super.onError(response);
                        showErrorView(response.code());
                    }
                });
    }


}
